package by.semerikov.dao.factory.mysqlJdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.mysql.jdbc.PreparedStatement;
 

import by.semerikov.dao.IDAO; 
import by.semerikov.dao.UserSession;
import by.semerikov.data.Connector;
import by.semerikov.exceptions.BadObjectException;

public class UserSessionDAO implements IDAO<UserSession> {

	private Connection con;

	PreparedStatement insertStatement;

	public UserSessionDAO() throws SQLException {

		con = Connector.getConnection();
	}

	public UserSessionDAO(Connection con) {
		this.con = con;
	}

	@Override
	public void create(UserSession object) throws BadObjectException {
		try {
			PreparedStatement preparedState = (PreparedStatement) con
					.prepareStatement("insert into transportsystem.user_session (userId,user_session.index) values(?,?)");
			preparedState.setInt(1, object.getUserId());
			preparedState.setString(2, object.getIndex());
			preparedState.executeUpdate();
		} catch (Exception e) {
			throw new BadObjectException(e);
		}
	}

	@Override
	public List<UserSession> read(UserSession object) throws BadObjectException {
		ArrayList<UserSession> newList = new ArrayList<UserSession>();
		try {
			PreparedStatement preparedState = doReadPreparedStatement(object,
					"SELECT * FROM transportsystem.user_session ");
			ResultSet resultset = preparedState.executeQuery();
			while (resultset.next()) {
				newList.add(new UserSession(Integer.parseInt(resultset
						.getString("id")), resultset.getInt("userId"),
						resultset.getString("index")));
			}
			return newList;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	private PreparedStatement doReadPreparedStatement(UserSession object,
			String begin) throws SQLException{
		String newStr = begin;
		Map<String, Integer> map = new HashMap<String, Integer>();
		int number = 1;
		if (object.getId() != null) {
			newStr += "where  id = ?";
			map.put("id", number);
			number++;
		}
		if (object.getUserId() != null) {
			if (number == 1)
				newStr += " where ";
			if (number != 1)
				newStr += " And ";
			newStr += " userId = ?";
			map.put("userId", number);
			number++;
		}
		if (object.getIndex() != null) {
			if (number == 1)
				newStr += " where ";
			if (number != 1)
				newStr += " And ";
			newStr += " user_session.index = ?";
			map.put("user_session.index", number);
			number++;
		}
		
		PreparedStatement newPS = (PreparedStatement) con
				.prepareStatement(newStr);
		
		if (map.get("id") != null) {
			newPS.setInt(map.get("id"), object.getId());
		}
		if (map.get("userId") != null) {
			newPS.setInt(map.get("userId"),  object.getUserId());
		}
		if (map.get("user_session.index") != null) {
			newPS.setString(map.get("user_session.index"),  object.getIndex());
		}
		
		return newPS;
	}

	@Override
	public void update(UserSession object) {
		// TODO Auto-generated method stub

	}

	@Override
	public void delete(UserSession object) {
		try {
			PreparedStatement preparedState = doReadPreparedStatement(object,
					"DELETE FROM transportsystem.user_session ");
			preparedState.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
